Imports System.Reflection
Imports Softelvdm.Controls
Imports Softelvdm.SftTreeNET

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    sftTree1.Initializing = True
    sftTree1.Columns.Count = 4
    ' This sample demonstrates how the various parts of a tree control are used in cells.
    ' To prepare for this sample, create a new project with a blank form and add
    ' a SftTree/NET control named SftTree1.
    ' In addition, adjust the following FromFile method to use a (small) bitmap
    ' that is located on your system.
    Dim img As Image = Bitmap.FromFile("..\\..\\test.gif")

    ' Add an item
    Dim item As ItemClass = sftTree1.ItemCollection.Add()

    ' Add a text part to the first cell (could use CellBaseClass.Text instead)
    Dim cell As CellClass = item.Cells(0)
    Dim tp As TextPartClass = New TextPartClass("The first cell", LineStyleEnum.Wordwrap, HAlignmentOptionalEnum.Left, _
             VAlignmentOptionalEnum.Top, Color.Blue, Color.Green, Color.Red, Color.Blue, New Font("Arial", 12), StringFormatFlags.LineLimit Or StringFormatFlags.MeasureTrailingSpaces)
    cell.Parts.Add(tp)
    ' Followed by a check box (still in the first cell)
    Dim cb As CheckBoxPartClass = New CheckBoxPartClass(CheckBoxStateEnum.Unchecked, HAlignmentOptionalEnum.Left, VAlignmentOptionalEnum.Top, True)
    AddHandler cb.Action, AddressOf cb_Action
    cell.Parts.Add(cb)
    ' Followed by an image (still in the first cell)
    Dim ip As ImagePartClass = New ImagePartClass(img)
    cell.Parts.Add(ip)

    ' Add contents to the second cell
    item.Cells(1).Text = "The" & vbCrLf & "easy" & vbCrLf & "way"

    ' Add an image to the third cell
    cell = item.Cells(2)
    cell.Image = img
    ip = cell.Parts(0)
    AddHandler ip.Action, AddressOf ip_Action

    ' Add another item
    item = sftTree1.ItemCollection.Add()

    ' Add a progressbar to the first cell
    Dim pbp As ProgressBarPartClass = New ProgressBarPartClass(0, 50, 25, 100, 12, GadgetAppearanceEnum.ThemedSystem, _
                Color.Red, Color.White, System.Drawing.Drawing2D.LinearGradientMode.Vertical)
    pbp.PartAlignment = PartAlignmentEnum.EntireArea
    pbp.Appearance = GadgetAppearanceEnum.System
    item.Cells(0).Parts.Add(pbp)

    ' Add a button to the second cell
    Dim bp As ButtonPartClass = New ButtonPartClass(False, HAlignmentOptionalEnum.Default, VAlignmentOptionalEnum.Default, _
            True, False, GadgetAppearanceEnum.ThemedSystem, Nothing, "Click Me", Nothing, Color.Red)
    AddHandler bp.Action, AddressOf bp_Action
    item.Cells(1).Parts.Add(bp)

    ' Add text to the third cell
    cell = item.Cells(2)
    cell.Text = "Sample Text"
    ' Add a dropdown button (still in the third cell)
    Dim ddp As DropDownButtonPartClass = New DropDownButtonPartClass()
    AddHandler ddp.Action, AddressOf ddp_Action
    ddp.PartAlignment = PartAlignmentEnum.FlushEnd
    cell.Parts.Add(ddp)

    ' Add another item
    item = sftTree1.ItemCollection.Add()

    ' Add three parts with a vertical orientation to the first cell
    cell = item.Cells(0)
    cell.Orientation = OrientationOptionalEnum.Vertical
    tp = New TextPartClass("First part")
    tp.HAlign = HAlignmentOptionalEnum.Left
    cell.Parts.Add(tp)
    ip = New ImagePartClass(img)
    ip.HAlign = HAlignmentOptionalEnum.Center
    cell.Parts.Add(ip)
    tp = New TextPartClass("Third part")
    tp.HAlign = HAlignmentOptionalEnum.Right
    cell.Parts.Add(tp)

    ' Add three parts with a vertical orientation to the second cell
    cell = item.Cells(1)
    cell.Orientation = OrientationOptionalEnum.Vertical
    tp = New TextPartClass("First part")
    tp.HAlign = HAlignmentOptionalEnum.Center
    cell.Parts.Add(tp)
    ip = New ImagePartClass(img)
    ip.HAlign = HAlignmentOptionalEnum.Center
    cell.Parts.Add(ip)
    tp = New TextPartClass("Third part")
    tp.HAlign = HAlignmentOptionalEnum.Center
    cell.Parts.Add(tp)

    ' Add three parts with a vertical orientation to the third cell
    cell = item.Cells(2)
    cell.Orientation = OrientationOptionalEnum.Vertical
    tp = New TextPartClass("First part")
    tp.HAlign = HAlignmentOptionalEnum.Right
    cell.Parts.Add(tp)
    ip = New ImagePartClass(img)
    ip.HAlign = HAlignmentOptionalEnum.Center
    cell.Parts.Add(ip)
    tp = New TextPartClass("Third part")
    tp.HAlign = HAlignmentOptionalEnum.Left
    cell.Parts.Add(tp)

    ' Set a sorting indicator in the first column header
    cell = sftTree1.Headers(0, 0)
    cell.SortIndicatorPosition = SortIndicatorPositionEnum.Side
    cell.SortStatus = SortStatusEnum.Ascending

    sftTree1.Columns.MakeOptimal(0, False)
    sftTree1.RecalcHorizontalExtent()
    sftTree1.Initializing = False
End Sub

' Dropdown button Action event
Private Sub ddp_Action(ByVal sender As Object, ByVal e As ActionEventArgs)
    Dim ddp As DropDownButtonPartClass = e.Part
    MessageBox.Show("Dropdown button clicked.")
End Sub

' Button part Action event
Private Sub bp_Action(ByVal sender As Object, ByVal e As ActionEventArgs)
    ' this is our button that was just clicked
    Dim bp As ButtonPartClass = e.Part
    ' this is the cell containing the button
    Dim cell As CellClass = bp.PartOwner
    ' get the previous cell
    cell = cell.Previous
    ' this cell's first (and only) part is a progressbar (in this example)
    Dim pbp As ProgressBarPartClass = cell.Parts(0)
    ' increment the progressbar's Value property
    Dim Val As Integer
    If pbp.Value = pbp.Max Then
        Val = 0
    Else
        Val = pbp.Value + 10
    End If
    Val = Math.Min(Val, pbp.Max)
    pbp.Value = Val
End Sub

' Checkbox part Action event
Private Sub cb_Action(ByVal sender As Object, ByVal e As ActionEventArgs)
    MessageBox.Show("Clicked the checkbox.")
End Sub

' Image part Action event
Private Sub ip_Action(ByVal sender As Object, ByVal e As ActionEventArgs)
    MessageBox.Show("Clicked the image.")
End Sub

' ItemClick event
Private Sub sftTree1_ItemClick(ByVal sender As Object, ByVal e As Softelvdm.SftTreeNET.ItemClickEventArgs) Handles sftTree1.ItemClick
    Debug.Write("** ItemClick")
    DumpValues(e)
End Sub

' This is a small helper routine to show all properties and fields of an object
Private Sub DumpValues(ByVal o As Object)
    Dim api() As PropertyInfo = o.GetType().GetProperties()
    For Each pi As PropertyInfo In api
        Debug.Write(" " & pi.Name & " " & pi.GetValue(o, New Object() {}))
    Next
    Dim afi() As FieldInfo = o.GetType().GetFields()
    For Each fi As FieldInfo In afi
        Dim t As Object = fi.GetValue(o)
        Dim s As String = "(null)"
        If Not t Is Nothing Then s = t.ToString()
        Debug.Write(" " & fi.Name & " " & s)
    Next
    Debug.WriteLine("")
End Sub
End Class